export default function (type = 'checkbox') { // checkbox
  const iconProps = type === 'switch' ? {} : { uncheckIcon: String, checkedIcon: String };
  return {
    inheritAttrs: false,
    inject: {
      muFormItem: {
        default: ''
      }
    },
    model: {
      prop: 'inputValue',
      event: 'change'
    },
    props: {
      label: String,
      readonly: Boolean,
      ...iconProps,
      disabled: Boolean
    },
    methods: {
      handleClick (e) {
        if (this.disabled || this.readonly) return;
        this.toggle();
        this.muFormItem && this.muFormItem.onBlur();
        this.$emit('click', e);
      },
      createInputElement(h) {
        return h('input', {
          attrs: {
            ...this.$attrs,
            type: type === 'switch' ? 'checkbox' : type,
            disabled: this.disabled,
            checked: this.checked,
            readonly: this.readonly,
            tabindex: -1
          }
        });
      },
      createSelect (h, view) {
        const label = this.label ? h('div', { staticClass: `co-${type}-label` }, this.label) : undefined;
        const wrapper = h('div', {
          staticClass: `co-${type}-wrapper`
        }, [view, label]);

        return h('div', {
          staticClass: `co-${type}`,
          class: {
            [`co-${type}--disabled`]: this.disabled,
            [`co-${type}--checked`]: this.checked,
            [`co-${type}--no-label`]: !this.label
          },
          on: {
            ...this.$listeners,
            click: this.handleClick
          }
        }, [
          this.createInputElement(h),
          wrapper
        ]);
      }
    }
  };
}
